using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Windows.Forms;

namespace yW5Lib
{
	public class clsPreserveHTM
	{
		public Collection colHtml;

		public clsPreserveHTM()
		{
			colHtml = new Collection();
		}

		public void PreserveHtml(ref string RTF)
		{
			colHtml.Clear();
			RichTextBox richTextBox = new RichTextBox();
			try
			{
				richTextBox.Rtf = RTF;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				richTextBox.Text = RTF;
				ProjectData.ClearProjectError();
			}
			int num = 0;
			for (int num2 = richTextBox.Find("<HTM "); num2 > -1; num2 = richTextBox.Find("<HTM "))
			{
				int num3 = richTextBox.Find(" /HTM>");
				if (num3 <= num2)
				{
					break;
				}
				richTextBox.Select(num2, num3 - num2 + 6);
				string selectedText = richTextBox.SelectedText;
				if (Operators.CompareString(selectedText, "", TextCompare: false) != 0)
				{
					num++;
					selectedText = Strings.Mid(selectedText, 5);
					selectedText = Strings.Left(selectedText, Strings.Len(selectedText) - 5);
					selectedText = selectedText.Trim();
					colHtml.Add(selectedText);
					richTextBox.SelectedText = "[[h!" + Conversions.ToString(num) + "!]]";
				}
			}
			RTF = richTextBox.Rtf;
		}

		public void RestoreHtml(ref string RTF)
		{
			int count = colHtml.Count;
			for (int i = 1; i <= count; i++)
			{
				string replacement = Conversions.ToString(colHtml[i]);
				RTF = Strings.Replace(RTF, "[[h!" + Conversions.ToString(i) + "!]]", replacement);
			}
		}
	}
}
